<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" width="70vw" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back" v-if="type != 1">确定</el-button>
                    <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">新增</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-checkbox v-model="is_stock" size="large" label="隐藏零库存" @change="stockChange" />
                        <el-input v-model="search.name" placeholder="商品名称" clearable @keyup.enter="upsearch"></el-input>
                        <el-input v-model="search.code" placeholder="货号" clearable @keyup.enter="upsearch"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" @click="unsetSearch">重置</el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" @select-all="selectAll" @selection-change="selectionChange" @row-dblclick="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                    <el-table-column type="selection" width="50" reserve-selection align="center" v-if="type != 1"></el-table-column>
                    <el-table-column type="index" label="" fixed align="center" width="50"></el-table-column>
                    <el-table-column label="商品名称" prop="name"></el-table-column>
                    <el-table-column label="货号" prop="code"></el-table-column>
					<el-table-column label="体积" prop="volume"></el-table-column>
                </scTable>
            </el-main>
        </el-container>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
        </template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
    </el-dialog>
</template>

<script>
import saveAdd from './save.vue'

export default {
    components: {saveAdd},
    props: {
        query: {type: Object, default: () => ({})},
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '选择商品',
                edit: '选择商品',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            dialog: {
				save: false
			},
			apiObj: this.$API.basic.goods.list,
			selection: [],
			search: {
				keyword: null,
                has_stock: 1,
			},
            type: "",
            is_stock: true,
        }
    },
    mounted() {
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        // 表单注入数据
        setData(data) {
            this.type = data
        },
        selectAll() {
            if (this.type == 1) {
                this.$message.error("只能单选！")
                this.$refs.table.clearSelection()
            }
        },
        // 选择事件
        selectionChange(selection) {
            if (this.type == 1) {
                if (selection.length > 1) {
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row, false) //设置这一行取消选中
                }
                this.$emit('success', selection, this.type)
                this.visible = false;
            }
            this.selection = selection;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection, this.type)
            this.visible = false;
        },
        //商品新增
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        // 更新商品新增
        handleSuccess() {
            this.$refs.table.refresh()
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        stockChange(e) {
            this.search.has_stock = e ? 1 : 0
            this.$refs.table.upData(this.search)
        }
    }
}
</script>

<style></style>
